﻿@model ShoppingCartModel
@using Nop.Core
@using Nop.Core.Infrastructure
@using Nop.Web.Models.ShoppingCart;
@{
    var webHelper = EngineContext.Current.Resolve<IWebHelper>();
}
<!-- shopping cart product list -->
<table id="cart-products-module" class="cart-products-module">
    <tr class="cart-header-row">
        @if (Model.ShowSku)
        {
            <th class="sku">@T("ShoppingCart.SKU")</th>
        }
        @if (Model.ShowProductImages)
        {
            <th class="picture">Picture</th>
        }
        <th class="products">@T("ShoppingCart.Product(s)")</th>
        <th class="unitprice">@T("ShoppingCart.UnitPrice")</th>
        <th class="qty">@T("ShoppingCart.Quantity")</th>
        <th class="subtotal">@T("ShoppingCart.ItemTotal")</th>
    </tr>
    @foreach (var item in Model.Items)
    {
        <tr class="cart-item-row">
            @if (Model.ShowSku)
            {
                <td class="sku">@item.Sku</td>
            }
            @if (Model.ShowProductImages)
            {
                <td class="picture">
                    <div>
                        <img alt="@item.Picture.AlternateText" src="@item.Picture.ImageUrl" title="@item.Picture.Title" />
                    </div>
                </td>
            }
            <td class="products">
                <div class="product-name">
                    <a href="@Url.RouteUrl("Product", new { SeName = item.ProductSeName })" title="@item.ProductName" class="product-name">@item.ProductName.ToStringLength(50)</a>
                </div>
                @if (!String.IsNullOrEmpty(item.AttributeInfo))
                {
                    <div class="product-attributes">
                        @Html.Raw(item.AttributeInfo)
                    </div>
                }
                @if (!String.IsNullOrEmpty(item.RecurringInfo))
                {
                    <div class="recurring-info">
                        @Html.Raw(item.RecurringInfo)
                    </div>
                }
                @if (!String.IsNullOrEmpty(item.RentalInfo))
                {
                    <div class="rental-info">
                        @Html.Raw(item.RentalInfo)
                    </div>
                }
                @if (Model.IsEditable && item.AllowItemEditing)
                {
                    var editCartItemUrl = Url.RouteUrl("Product", new { SeName = item.ProductSeName });
                    editCartItemUrl = webHelper.ModifyQueryString(editCartItemUrl, "updatecartitemid=" + item.Id, null);
                    <div class="edit-item">
                        <a href="@editCartItemUrl">@T("Common.Edit")</a>
                    </div>
                }
                @if (item.Warnings.Count > 0)
                {
                    <div class="message-error">
                        <ul>
                            @for (int i = 0; i < item.Warnings.Count; i++)
                            {
                                <li>@item.Warnings[i]</li>
                            }
                        </ul>
                    </div>
                }
            </td>
            <td class="unitprice">
                <span class="amount">@item.UnitPrice</span>
            </td>
            <td class="qty">
                <div class="wrap">
                    @if (Model.IsEditable)
                    {
                        if (item.AllowedQuantities.Count > 0)
                        {
                            <select name="itemquantity@(item.Id)" class="qty-dropdown form-control">
                                @foreach (var qty in item.AllowedQuantities)
                                {
                                    <option selected="@qty.Selected" value="@qty.Value">@qty.Value</option>
                                }
                            </select>
                        }
                        else
                        {
                            <span data-action="decrease" class="decrease">-</span>
                            <input name="itemquantity@(item.Id)" type="text" value="@(item.Quantity)" class="value qty-input" />
                            <span data-action="increase" class="increase">+</span>
                        }
                    }
                    else
                    {
                        <span class="value">@item.Quantity</span>
                    }
                </div>
            </td>
            <td class="subtotal">
                <span class="amount">@item.SubTotal</span>
                @if (!String.IsNullOrEmpty(item.Discount))
                {
                    <div class="discount">
                        @T("ShoppingCart.ItemYouSave"): @item.Discount
                    </div>
                }
                @if (Model.IsEditable)
                {
                    <span class="remove">
                        @T("ShoppingCart.Remove")
                    </span>
                    <span class="remove-chkbox"><input type="checkbox" name="removefromcart" value="@(item.Id)" /></span>
                }
            </td>
        </tr>
    }

</table>
@if (Model.IsEditable && Model.Items.Count > 0)
{
    <script type="text/javascript">
        seajs.use("common/1.0.0/index", function (app) {
            app(function () {
                var $cartlist = app("#cart-products-module");
                // delegate increase, decrease button.
                $cartlist.on("click", "span[data-action]", function () {
                    var $this = app(this);
                    var action = $this.data("action");
                    if (action == "increase") {
                        // plus.
                        var $qtyInput = $this.prev("input.qty-input");
                        var orginVal = parseInt($qtyInput.val());
                        $qtyInput.val(++orginVal);
                    } else {
                        var $qtyInput = $this.next("input.qty-input");
                        var orginVal = parseInt($qtyInput.val());
                        if (orginVal > 1) {
                            $qtyInput.val(--orginVal);
                        }
                    }
                });
                // delegate remove button.
                $cartlist.on("click", "span.remove", function () {
                    var $this = app(this);
                    var $chckbox = $this.next(".remove-chkbox").find("input[type='checkbox']");
                    $chckbox.prop("checked", true);
                    // trigger form update button.
                    var $cartUpdateBtn = app("#updatecartbtn");
                    if ($cartUpdateBtn.length) {
                        $cartUpdateBtn.trigger("click");
                    }
                });
            });
        });
    </script>
}

